<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Project extends Admin_Controller {

	function __construct ()
	{
		parent::__construct();
        $this->load->model('authGroup_m');
        $this->load->model('project_m');
        $this->load->model('defray_m');
        $this->load->model('userinto_m');
        $this->load->model('settlement_m');
        $this->load->model('charge_m');
        $this->load->library('pagination');
		if(!$this->auth->is_admins()){
			redirect(site_url('user/login'));
		}
        $user_id = $this->session->userdata('user_id');
        $user = $this->user_m->get_user_by_user_id($user_id);
        $data['user'] = $user;
	}
	
	public function index($page=1)
	{
        $tagid = explode('|', $this->config->item('tag'));
        $tag = explode('|', $this->config->item('tag_text'));
        $tags=array_combine($tagid,$tag);
        $typeid = explode('|', $this->config->item('type'));
        $type = explode('|', $this->config->item('type_text'));
        $types=array_combine($typeid,$type);
        $use = $this->user_m->get_users();
        $user_id = $this->session->userdata('user_id');
        $user = $this->user_m->get_user_by_user_id($user_id);
        $users=array();
        if(!empty($use))
        foreach ($use as $kk=>$vv){
            $users[$vv['user_id']]=$vv['user_uname'];
        }
        $keywords = !empty($this->input->get('project_name'))?$this->input->get('project_name'):'';
        $limit = 30;
        $config['uri_segment'] = 4;
        $config['use_page_numbers'] = TRUE;
        $config['reuse_query_string'] = TRUE;
        $config['base_url'] = base_url('manage/project/index/');
        if($user['user_group']==4){
            $config['total_rows'] = $this->project_m->count_user_project_find($keywords,$user_id);
        }else{
            $config['total_rows'] = $this->project_m->count_project_find($keywords);
        }

        $config['per_page'] = $limit;
        $config['num_links'] = 10;
        $start = ($page-1)*$limit;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();

        if($user['user_group']==4){
            $data['projects'] = $this->project_m->get_user_project_list($start,$limit,$keywords,$user_id);
        }else{
            $data['projects'] = $this->project_m->get_project_list($start,$limit,$keywords);
        }

        if(!empty($data['projects'])){
            if(!empty($data['projects'])){
                foreach ($data['projects'] as $key => $val){
                    $pro_tags = explode(",",$val['project_tag']);
                    $pro_types = explode(",",$val['project_type']);
                    $pro_uids = explode(",",$val['project_user_id']);
                    $pro_tags_str=array();
                    $pro_types_str=array();
                    $pro_uses_str=array();
                    foreach ($pro_tags as $k => $v){
                        $pro_tags_str[] = $tags[$v];
                    }
                    $data['projects'][$key]['project_tags'] = $pro_tags_str;
                    foreach ($pro_types as $kk => $vv){
                        $pro_types_str[] = $types[$vv];
                    }
                    $data['projects'][$key]['project_types'] = $pro_types_str;
                    foreach ($pro_uids as $kkk => $vvv){
                        $pro_uses_str[$vvv] = $users[$vvv];
                    }
                    $data['projects'][$key]['project_uses'] = $pro_uses_str;
                    $time = strtotime($val['project_start_time']);
                    if($time<time() &&$val['project_status']==0){
                        $this->project_m->update_project($val['project_id'],array('project_status'=>1));
                    }
                }
            }
        }
		$this->load->view('project/index',$data);
	}
	//新增成员
    public function add(){
        $tagid = explode('|', $this->config->item('tag'));
        $tag = explode('|', $this->config->item('tag_text'));
        $data['tags']=array_combine($tagid,$tag);
        $tagid = explode('|', $this->config->item('type'));
        $tag = explode('|', $this->config->item('type_text'));
        $data['types']=array_combine($tagid,$tag);
        $data['use'] = $this->user_m->get_users();
	    $this->load->view('project/form',$data);
    }
    //更新信息
    public function update(){
        $post = $this->input->post();
        $project_id = isset($post['project_id']) ? intval($post['project_id']) : false;
        $projectname = isset($post['project_name']) ? htmlspecialchars($post['project_name'], ENT_QUOTES) : '';
        $project_end_time = isset($post['project_end_time']) ? $post['project_end_time'] :'';
        $project_start_time = isset($post['project_start_time']) ? $post['project_start_time'] :'';
        $tags=implode(',',$post['project_tag']);
        $types=implode(',',$post['project_type']);
        $projectuserid=implode(',',$post['project_user_id']);
        if(count($post['project_user_id'])!=count($post['into_num'])){
            show_message('参与人员和分成数必须一一对应',site_url(X_URL.'project/add'),1);
        }
        $userinto = array_combine($post['project_user_id'],$post['into_num']);
        $num=0;
        foreach ($post['into_num'] as $key=>$val){
            $num += $val;
        }
        if($num>1){
            show_message('分成数不能大于1',site_url(X_URL.'project/add'),1);
        }
        if (!$project_id) {
            $project = array(
                'project_name' => $projectname,
                'project_tag' =>$tags,
                'project_type' =>  $types,
                'project_user_id'=> $projectuserid,
                'project_customer'=> $post['project_customer'],
                'project_customer_tel' => $post['project_customer_tel'],
                'project_total_money' => $post['project_total_money'],
                'project_get_money'=>$post['project_get_money'],
                'project_money' =>$post['project_money'],
                'project_start_time' =>$project_start_time,
                'project_end_time' =>$project_end_time,
                'project_status'=>$post['project_status'],
            );
            if($post['project_status']==3 && $post['project_total_money']>$post['project_money']){
                $this->defray_m->add_defray(array('defray_title'=>$projectname,'defray_time'=>date("Y-m-d",time()),'defray_money'=>$post['project_total_money']-$post['project_money'],'defray_type'=>2));
            }
            $result = $this->project_m->add_project($project);
            if($result){
                foreach ($userinto as $key=>$val){
                    $intoinfo = array('project_id'=>$result,'user_id'=>$key,'into_num'=>$val);
                    $this->userinto_m->add_userinto($intoinfo);
                }
                $this->charge_m->add_charge(array('project_id'=>$result,'charge_money'=>$post['project_get_money'],'charge_time'=>time(),'charge_text'=>'首付款'));
                show_message('新增项目成功',site_url(X_URL.'project/index'),1);
            }else{
                show_message('新增项目失败',site_url(X_URL.'project/index'),1);
            }

        } else {
            // 修改
            $project = array(
                'project_name' => $projectname,
                'project_tag' =>$tags,
                'project_type' =>  $types,
                'project_user_id'=> $projectuserid,
                'project_customer'=> $post['project_customer'],
                'project_customer_tel' => $post['project_customer_tel'],
                'project_total_money' => $post['project_total_money'],
                'project_money' =>$post['project_money'],
                'project_update_time' =>time(),
                'project_status'=>$post['project_status'],
            );
            if(!empty($project_start_time)){
                $project['project_start_time']=$project_start_time;
            }
            if(!empty($project_end_time)){
                $project['project_end_time']=$project_end_time;
            }
            if($post['project_status']==3 && $post['project_total_money']>$post['project_money']){
                $this->defray_m->add_defray(array('defray_title'=>$projectname,'defray_time'=>date("Y-m-d",time()),'defray_money'=>$post['project_total_money']-$post['project_money'],'defray_type'=>2));
            }
            $result = $this->project_m->update_project($project_id,$project);
            if($result){
                foreach ($userinto as $key=>$val){
                    $intoinfo = array('project_id'=>$project_id,'user_id'=>$key,'into_num'=>$val);
                    $this->userinto_m->add_userinto($intoinfo);
                }
                show_message('修改成功',site_url(X_URL.'project/index'),1);
            }else{
                show_message('修改失败',site_url(X_URL.'project/index'),1);
            }
        }
    }
    public function edit($project_id){
        if ($project_id) {
            $project = $this->project_m->get_one_project($project_id);
        } else {
            show_message('参数错误',site_url(X_URL.'project/index'),1);
        }
        $project['project_tag'] = explode(',',$project['project_tag']);
        $project['project_type'] = explode(',',$project['project_type']);
        $project['project_user_id'] = explode(',',$project['project_user_id']);
        $data['project'] = $project;
        $tagid = explode('|', $this->config->item('tag'));
        $tag = explode('|', $this->config->item('tag_text'));
        $data['tags']=array_combine($tagid,$tag);
        $tagid = explode('|', $this->config->item('type'));
        $tag = explode('|', $this->config->item('type_text'));
        $data['types']=array_combine($tagid,$tag);
        $data['use'] = $this->user_m->get_users();
        if(!empty($data['use'])){
            foreach ($data['use'] as $key=>$val){
                $userinto = $this->userinto_m->get_project($val['user_id'],$project_id);
                $data['use'][$key]['into_num']=$userinto['into_num'];
            }
        }
        $this->load->view('project/form',$data);
    }

    public function delete($project_id){
        if($project_id){
            $result = $this->project_m->update_project($project_id,array('delete'=>1));
            if($result){
                //删除进度
                $this->userinto_m->update_userinto_by_project_id_id($project_id,array('delete'=>1));
                //删除结算
                $this->settlement_m->update_project_settlement($project_id,array('delete'=>1));
                //删除收费
                $this->charge_m->update_project_charge($project_id,array('delete'=>1));
                show_message('删除成功',site_url(X_URL.'project/index'),1);
            }else{
                show_message('删除失败',site_url(X_URL.'project/index'),1);
            }
        }else{
            show_message('参数错误',site_url(X_URL.'project/index'),1);
        }
    }


}